#include "inputdialog.h"

#include <QtGui/QComboBox>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QPushButton>
#include <QtGui/QVBoxLayout>
#include <QtGui/QValidator>

using namespace gui;

InputDialog* inputDialog = 0;
QPushButton* InputDialog::m_okButton = 0;

QString InputDialog::getText(QWidget *parent, const QString &title,
    const QString &label, const QString &inputMask,
    QSharedPointer<QValidator> validator, QLineEdit::EchoMode mode,
    const QString &text, bool *ok)
{
    if (inputDialog == 0)
    {
        inputDialog = new InputDialog();
    }

    QString ret;

    InputDialog* dialog = new InputDialog(parent);

    QVBoxLayout* mainLayout = new QVBoxLayout;
    QHBoxLayout* buttonsLayout = new QHBoxLayout;

    QLabel* labelWidget = new QLabel(label);

    QLineEdit* lineEditWidget = new QLineEdit;
    lineEditWidget->setEchoMode(mode);
    lineEditWidget->setText(text);
    lineEditWidget->setFocus(Qt::PopupFocusReason);
    lineEditWidget->selectAll();
    if (!inputMask.isEmpty())
    {
        lineEditWidget->setInputMask(inputMask);
    }
    if (!validator.isNull())
    {
        lineEditWidget->setValidator(validator.data());
    }

    labelWidget->setBuddy(lineEditWidget);

    mainLayout->addWidget(labelWidget);
    mainLayout->addWidget(lineEditWidget);

    m_okButton = new QPushButton(tr("OK"));
    m_okButton->setDefault(true);

    QPushButton* cancelButton = new QPushButton(tr("Cancel"));

    buttonsLayout->addWidget(m_okButton);
    buttonsLayout->addWidget(cancelButton);
    buttonsLayout->addStretch();

    mainLayout->addLayout(buttonsLayout);
    dialog->setLayout(mainLayout);

    dialog->setWindowTitle(title);

    connect(m_okButton, SIGNAL(clicked()),
            dialog, SLOT(accept()));
    connect(cancelButton, SIGNAL(clicked()),
            dialog, SLOT(reject()));
    connect(lineEditWidget, SIGNAL(textChanged(QString)), inputDialog,
        SLOT(checkText(QString)));

    const int maxSize = dialog->sizeHint().height();
    dialog->setMinimumHeight(maxSize);
    dialog->setMaximumHeight(maxSize);

    int status = dialog->exec();

    if (status == QDialog::Accepted)
    {
        ret = lineEditWidget->text();
    }

    if (ok != 0)
    {
        *ok = (bool)status;
    }

    delete dialog;
    m_okButton = 0;

    delete inputDialog;
    inputDialog = 0;

    return ret;
}

QString InputDialog::getTextFromConstraints(QWidget* parent,
    const QString& title, const QString& label,
    const QStringList& constraints, bool* ok)
{
    if (inputDialog == 0)
    {
        inputDialog = new InputDialog();
    }

    QString ret;

    InputDialog* dialog = new InputDialog(parent);

    QVBoxLayout* mainLayout = new QVBoxLayout;
    QHBoxLayout* buttonsLayout = new QHBoxLayout;

    QLabel* labelWidget = new QLabel(label);

    QComboBox* constraintsWidget = new QComboBox;
    constraintsWidget->addItems(constraints);
    constraintsWidget->setFocus();

    labelWidget->setBuddy(constraintsWidget);

    mainLayout->addWidget(labelWidget);
    mainLayout->addWidget(constraintsWidget);

    m_okButton = new QPushButton(tr("OK"));
    m_okButton->setDefault(true);

    QPushButton* cancelButton = new QPushButton(tr("Cancel"));

    buttonsLayout->addWidget(m_okButton);
    buttonsLayout->addWidget(cancelButton);
    buttonsLayout->addStretch();

    mainLayout->addLayout(buttonsLayout);
    dialog->setLayout(mainLayout);

    dialog->setWindowTitle(title);

    connect(m_okButton, SIGNAL(clicked()),
            dialog, SLOT(accept()));
    connect(cancelButton, SIGNAL(clicked()),
            dialog, SLOT(reject()));

    const int maxSize = dialog->sizeHint().height();
    dialog->setMinimumHeight(maxSize);
    dialog->setMaximumHeight(maxSize);

    int status = dialog->exec();

    if (status == QDialog::Accepted)
    {
        ret = constraintsWidget->currentText();
    }

    if (ok != 0)
    {
        *ok = (bool)status;
    }

    delete dialog;
    m_okButton = 0;

    delete inputDialog;
    inputDialog = 0;

    return ret;
}

InputDialog::InputDialog(QWidget* parent) : QDialog(parent),
    m_ok(false)
{

}

InputDialog::~InputDialog()
{

}

// private slots
void InputDialog::checkText(const QString& text)
{
    m_okButton->setEnabled(!text.isEmpty());
}
